<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %>
<%
    String path = request.getContextPath();
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>打印&冲红&作废</title>
    <script src="http://code.jquery.com/jquery-3.4.1.min.js"
            integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
            crossorigin="anonymous"></script>
    <jsp:include page="../../import.jsp" flush="true"/>
    <jsp:include page="activeX4Invoice.jsp" flush="true"/>
    <OBJECT ID=sk
            CLASSID="clsid:003BD8F2-A6C3-48EF-9B72-ECFD8FC4D49F"
            codebase="NISEC_SKSCX.ocx#version=1,0,0,1" style="display: none">
    </OBJECT>
</head>
<body class="iframeBody">

<div class="Wrapper">
    <div>
        <div id="pageInTab" class="pageInTab">
            <form id="cust_auditing_add_form">
                <div class="fieldset1 tabel_col clearFix">
                    <div class="fieldBox col_33">
                        <div class="label">客户名称</div>
                        <div class="field">
                            <input name="cstNm" id="cstNm" disabled="disabled" type="text" class="ui-textbox" value=""
                                   validate="{required:true}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">项目所属机构</div>
                        <div class="field">
                            <input name="orgNm" id="orgNm" disabled="disabled" type="text" class="ui-textbox"
                                   validate="{required:true}">
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">收件人</div>
                        <div class="field">
                            <input name="rcverNm" id="rcverNm" type="text" class="ui-textbox" value=""
                                   placeholder="无特殊情况,请填写商助姓名." validate="{required:true, maxlength:10}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">联系电话</div>
                        <div class="field">
                            <input name="rcverTel" id="rcverTel" type="text" class="ui-textbox" value=""
                                   placeholder="无特殊情况,请填写商助电话." validate="{required:true, maxlength:15}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">流程发起人姓名</div>
                        <div class="field">
                            <input name="sysUsrRlNm" id="sysUsrRlNm" type="text" disabled="disabled" class="ui-textbox"
                                   value="" validate="{required:true, maxlength:10}">
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">流程发起人联系电话</div>
                        <div class="field">
                            <input name="sysUsrMp" id="sysUsrMp" type="text" disabled="disabled" class="ui-textbox"
                                   value="" validate="{required:true, maxlength:15}">
                        </div>
                    </div>
                    <div class="fieldBox col_100">
                        <div class="label">收件地址</div>
                        <div class="field">
                            <input name="rcvAddr" id="rcvAddr" type="text" class="ui-textbox" value=""
                                   placeholder="无特殊情况,请填写商助地址." validate="{required:true, maxlength:60}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>

                    <div class="fieldBox col_33">
                        <div class="label">快递公司</div>
                        <div class="field">
                            <input name="expCorp" id="expCorp" type="text" class="ui-textbox" value=""
                                   validate="{required:true, maxlength:25}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>

                    <div class="fieldBox col_33">
                        <div class="label">快递单号</div>
                        <div class="field">
                            <input name="trcNo" id="trcNo" type="text" class="ui-textbox" value=""
                                   validate="{required:true, maxlength:30}">
                            <span class="requiredSymbol">*</span>
                        </div>
                    </div>

                    <div class="fieldBox col_33">
                        <div class="label">是否提前开票</div>
                        <div class="field">
                            <span id="isPreInvNm" name="isPreInvNm"></span>
                        </div>
                    </div>

                    <div class="fieldBox col_33">
                        <div class="label">开票人电话</div>
                        <div class="field">
                            <input name="invTel" id="invTel" disabled="disabled" type="text" class="ui-textbox" value=""
                                   validate="{required:false, maxlength:15}">
                        </div>
                    </div>

                    <div class="fieldBox col_33">
                        <div class="label">开票人开户行</div>
                        <div class="field">
                            <select name="invBkCd" id="invBkCd" data-value=""
                                    validate="{required:false, select: false}">
                                <option value="">请选择</option>
                                <c:forEach items="${selectBkCdList }" var="kv">
                                    <option value="${kv.key }">${kv.value }</option>
                                </c:forEach>
                            </select>
                        </div>
                    </div>

                    <div class="fieldBox col_33">
                        <div class="label">开票人帐号</div>
                        <div class="field">
                            <input name="invAccNo" id="invAccNo" disabled="disabled" type="text" class="ui-textbox"
                                   value="" validate="{required:false, maxlength:30}">
                        </div>
                    </div>

                    <div class="fieldBox col_33">
                        <div class="label">纳税人识别号</div>
                        <div class="field">
                            <input name="invTaxId" id="invTaxId" disabled="disabled" type="text" class="ui-textbox"
                                   value="" validate="{required:false, maxlength:30}">
                        </div>
                    </div>
                    <div class="fieldBox col_33">
                        <div class="label">开具方式</div>
                        <div class="field">
                            <input name="billWayCd" id="billWayCd" disabled="disabled" type="text" class="ui-textbox"
                                   value="纸质发票" >
                        </div>
                    </div>
                    <c:if test="${'4' == flag}">
                        <div class="fieldBox col_33">
                            <div class="label">开具方式</div>
                            <div class="field">
                                <input name="applyReason" id="applyReason" disabled="disabled" ltype="combobox" class="ui-textbox">
                            </div>
                        </div>
                    </c:if>
                    <div class="fieldBox col_100">
                        <div class="label">发票抬头</div>
                        <div class="field">
                            <input name="invTitle" id="invTitle" disabled="disabled" type="text" class="ui-textbox"
                                   value="${cstNm }" validate="{required:false, maxlength:20}">
                        </div>
                    </div>

                    <div class="fieldBox col_100">
                        <div class="label">开票地址</div>
                        <div class="field">
                            <input name="invAddr" id="invAddr" disabled="disabled" type="text" class="ui-textbox"
                                   value="" validate="{required:false, maxlength:100}">
                        </div>
                    </div>

                    <div class="fieldBox col_100">
                        <div class="label">资料</div>
                        <div class="field">
                            <jsp:include page="../../system/sysUlFilesRec/sysUlFileRecListDetail.jsp" flush="true">
                                <jsp:param name="pkId" value="${id }"/>
                                <jsp:param name="tabNm" value="PRJ_BILL_APPLY"/>
                            </jsp:include>
                            <span style="color: red;">提示:若申请开具增值税专用发票,需要上传营业执照或承租人开票信息附件.</span>
                        </div>
                    </div>

                    <!-- hide data -->
                    <div class="fieldBox col_100" style="display:none;">
                        <div class="field">
                            <input name="applyId" id="applyId" type="text" class="ui-textbox" value="${applyId}">
                            <input name="orgCd" id="orgCd" type="text" class="ui-textbox" value="${orgCd}">
                            <input name="isPreInv" id="isPreInv" type="text" class="ui-textbox" value="">
                        </div>
                    </div>
                </div>
            </form>
            <div class="sqPanel">
                <div class="sqPanel_head inL">
                    <span>支付表信息</span>
                </div>
                <!-- 支付表列表 -->
                <div id="invoiceAddGrid" class="sq-grid"></div>
            </div>
            <div class="btnsBox" style="text-align: center">
                <c:if test="${'2' == flag}">
                    <span id="printInvoice" class="sq-button sq-button-round" onclick="operateInvoice('${flag}')">打印</span>
                </c:if>
                <c:if test="${'3' == flag}">
                    <span id="invalidInvoice" class="sq-button sq-button-round" onclick="operateInvoice('${flag}')">作废</span>
                </c:if>
                <c:if test="${'4' == flag}">
                    <span id="revertInvoice" class="sq-button sq-button-round" onclick="operateInvoice('${flag}')">冲红</span>
                </c:if>
            </div>
        </div>
    </div>
</div>


<!-- 验证状态窗口-->
<div id="div_auditing_save_win_id" style="display:none">
    <form id="div_auditing_save_win_form">
        <div class="fieldset1 tabel_col tabel_col_s clearFix noBorder">
            <div class="fieldBox col_50">
                <div class="label">开票类型</div>
                <div class="field">
                    <select name="billTypCd" id="win_billtypcd_id" data-value=""
                            validate="{required:true, select: true}">
                        <c:forEach items="${prjBillStsCdList }" var="vs" begin="0" step="1" varStatus="status">
                            <option value="${vs.key }">${vs.value }</option>
                        </c:forEach>
                    </select>
                    <span class="requiredSymbol">*</span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">票据号</div>
                <div class="field">
                    <input name="billNo" id="billNo" type="text" class="ui-textbox" value=""
                           validate="{required:true, maxlength:30}">
                    <span class="requiredSymbol">*</span>
                </div>
            </div>
        </div>
    </form>
</div>

<script>
    var bscInfoId = '${id}';
    var billTypCd = '${billTypCd}';
    $(function () {
        initForm();
        initData();
        initGrid();
    });

    //初始化表单
    function initForm() {
        $("#cust_auditing_add_form").ligerForm({
            validate: true
        });
        $("#div_auditing_save_win_form").ligerForm({
            validate: true
        });
    }

    var grid;

    //初始化列表
    function initGrid() {
        var billTypCdList = [];//模块类型数据
        var applyId = $("#applyId").val();
        $("#invoiceAddGrid").ligerGrid({
            usePager: false,
            url: "<%=path%>/invoice/prjbillfeer/initList.do?bscInfoId=" + bscInfoId + "&applyId=" + applyId,
            method: 'get',
            columns: [{
                display: '业务模式',
                name: 'bizTypCdNm',
                width: 180,
                align: 'center'
            }, {
                display: '支付表号',
                name: 'paySchNo',
                width: 200,
                align: 'center',
                render: function (rowdata, rowindex, value, column) {
                    var h = '';
                    h = '<a href="javascript:detailBtn(';
                    h += "'" + rowdata.id + "',";
                    h += "'" + rowdata.paySchId + "',";
                    h += ')">' + value + '</a>'
                    return h;
                },
            }, {
                display: '费用项',
                name: 'feeTypCdNm',
                width: 150,
                align: 'center'
            }, {
                display: '资金流向',
                name: 'capFlowNm',
                width: 100,
                align: 'center'
            }, {
                display: '应收金额(元)',
                name: 'billAmt',
                width: 120,
                align: 'right',
                render: function (rowdata, index, value) {
                    return '&yen;' + FormatMoney(rowdata.billAmt / 100);
                }
            }, {
                display: '实收金额(元)',
                name: 'billAmt',
                width: 120,
                align: 'right',
                render: function (rowdata, index, value) {
                    return '&yen;' + FormatMoney(rowdata.billAmt / 100);
                }
            }, {
                display: '开票类型',
                name: 'billTypCdNm',
                width: 150,
                align: 'center'
            }, {
                display: '开票状态',
                name: 'billStsCdNm',
                width: 100,
                align: 'center'
            }, {
                display: '票据号',
                name: 'billNo',
                width: 160,
                align: 'left',
                editor: {type: 'text'}
            }, getGridOperColumnContent()],
            onBeforeSubmitEdit: function (e) {//重新赋值,要不然选中后离开会把key显示在前端
                /* if (e.selected && e.column.columnname == "billTypCdNm") {
                    e.record.billTypCd = e.value;
                    e.value = e.selected[e.column.editor.textField];
                } */
            },
            isDisabledSelect: function (item) {
                if (StringUtils.isNotEmpty(item.billStsCd) && item.billStsCd == CustomerConstant.CODE_PRJ_BILL_STS_CD_2) {
                    return true;
                }

                if (item.isDel == CustomerConstant.CODE_PRJ_BILL_R_IS_DEL_2) {
                    return true;
                }
            },
            width: '100%',
            //height:'100%',
            heightDiff: -2,
            root: 'rows',
            record: 'total',
            rownumbers: true,
            enabledEdit: false,
            checkbox: true,
            fixedCellHeight: false,
            usePagerOnLessPagesize: false,
            onAfterShowData: function (currentData) {
                SetIframe_height();
                $(".liger_tip").ligerTip();
            }
        });

        grid = liger.get("invoiceAddGrid");
        //grid.setHeight(100);
        grid.options.isSingleCheck = false;
    }

    //获取Grid操作列内容
    function getGridOperColumnContent() {
        var data = {
            display: '操作',
            isSort: false,
            align: 'left',
            width: 180,
            render: function (rowdata, rowindex, value) {
                var h = "";
                return h;
            }
        }
        return data;
    }

    //新增回调
    function saveCallback(msg) {
        if (msg.success == 1) {
            $.ligerDialog.hide();
            UIUtils.alertSuccess(msg.info);
            var _applyId = $("#applyId").val();
            grid.setParm("applyId", _applyId);
            grid.reload();

        } else {
            UIUtils.alertError(msg.info);
        }
    }

    //作废
    function stopUsedZF(_id, _billId) {
        //获取Grid数据
        var pass = true;
        var tipTitle = "";
        var _data = [_billId];
        if (StringUtils.isEmpty(_billId)) {
            pass = false;
        }

        if (!pass) {
            UIUtils.alertWarning("未开票项不能作废!");
            return;
        }

        $.ligerDialog.confirm(LogUtils.LOG_CUST_STOP_USED_ZF, function (flag) {
            if (flag == false) return;
            var url = '<%=path%>/invoice/prjbillbscinfo/stopUsedZF.do';
            AjaxUtils.ajaxPost(url, {ids: JSON.stringify(_data)}, stopUsedRowCallback);
        });
    }

    //作废回调
    function stopUsedRowCallback(msg) {
        if (msg.success == 1) {
            UIUtils.alertSuccess(msg.info);
            grid.reload();
        } else {
            UIUtils.alertWarning(msg.info);
        }
    }

    //不开票
    function delRow(_id) {
        $.ligerDialog.confirm(LogUtils.LOG_CUST_STOP_USED_BKP, function (flag) {
            if (flag == false) return;
            var url = '<%=path%>/invoice/prjbillfeer/removeToHoldById.do';
            AjaxUtils.ajaxPost(url, {id: _id}, delRowCallback);
        });
    }

    //不开票回调
    function delRowCallback(msg) {
        if (msg.success == 1) {
            UIUtils.alertSuccess(msg.info);
            grid.reload();
        } else {
            UIUtils.alertWarning(msg.info);
        }
    }

    //修改初始化数据
    function initData() {
        var applyId = $("#applyId").val();
        if (!applyId) return;
        var _url = '<%=path%>/invoice/prjbillapply/getById.do';
        AjaxUtils.ajaxGet(_url, {id: applyId}, initCallback);
    }

    //初始化回调
    function initCallback(msg) {
        if (msg.success == 1) {
            setData(msg.data);//设置数据
        } else {
            UIUtils.alertError(msg.info);
        }
    }

    //修改时候设置页面数据
    function setData(data) {
        var form = new liger.get("cust_auditing_add_form");
        form.setData({
            rcverNm: data.rcverNm,
            rcverTel: data.rcverTel,
            rcvAddr: data.rcvAddr,
            expCorp: data.expCorp,
            trcNo: data.trcNo,
            cstNm: data.cstNm,
            orgNm: data.buOrgCdNm,
            invTel: data.invTel,
            invAccNo: data.invAccNo,
            invTaxId: data.invTaxId,
            invTitle: data.invTitle,
            invAddr: data.invAddr,
            invBkCd: data.invBkCd,
            sysUsrRlNm: data.sysUsrRlNm,
            sysUsrMp: data.sysUsrMp
        });
        $("#isPreInvNm").html(data.isPreInvNm);
        liger.get("invBkCd").setDisabled(true);

    };

    //查看支付表详情
    function detailBtn(id, paySchId) {
        var editFlag = true;
        Add_navTab('tab_prj_quat_prps' + id, '查看支付表', '<%=path%>/project/prjprdpayschm/toPrjPrdPaySchMDetailPageBySchId.do?paySchId=' + paySchId);
    };

    // 对发票进行操作(2打印 3作废 4冲红)
    function operateInvoice(operFlag) {
        var confirmText;
        if ("2" == operFlag) {
            confirmText = "是否确认打印纸质发票";
        } else if ("3" == operFlag) {
            confirmText = "是否确认作废发票";
        } else if ("4" == operFlag) {
            confirmText = "是否确认冲红发票";
        }
        $.ligerDialog.confirm(confirmText, function (flag) {
            if (flag == false) return;
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: {operFlag: operFlag, bscInfoId: bscInfoId},
                url: '<%=path%>/invoice/prjbillapply/getInvoiceXml.do',
                success: function (msg) {
                    if (msg.success == 1) {
                        if ("2" == operFlag) {
                            var paramSet = msg.data[0];
                            var pageSet = msg.data[1];
                            var printSet = msg.data[2];
                            var printResult = printInvoiceByActiveX(paramSet, pageSet, printSet);
                            if (!printResult) {
                                UIUtils.alertError("发票打印失败，请检查税控钥匙");
                                return false;
                            }
                        } else if ("3" == operFlag || "4" == operFlag) {
                            if ('4' != billTypCd) {
                                var keyQueryXml = msg.data[0];
                                var returnKeyQuery = keyQuery(keyQueryXml);
                                if (!returnKeyQuery) {
                                    return returnKeyQuery;
                                }
                            }
                            var _url = '<%=path%>/invoice/prjbillapply/invalidInvoice.do';
                            AjaxUtils.ajaxPost(_url, {bscInfoId: bscInfoId}, function(result){
                                if (1 == result.success) {
                                    $.ligerDialog.SQalert({content: result.info, SQ_type: "success"});
                                    parent.$("#searchBtn").click();
                                    Remove_navTab();
                                } else {
                                    $.ligerDialog.SQalert({content: result.info, SQ_type: "error"});
                                }
                            });
                        }
                    } else {
                        $.ligerDialog.SQalert({content: "获取发票报文失败", SQ_type: "error"});
                    }
                }
            });
        });
    }

</script>

</body>
</html>